import RECORDMANAGER.ReturnData;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.net.Socket;

/**
 * @author cz
 */
class RegionClientThread extends Thread {
    Socket socket;
    ObjectOutputStream objectOutputStream = null;

    public RegionClientThread(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            this.objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
            String result = bufferedReader.readLine();
            ReturnData returnData = Interpreter.interpret(result);
            objectOutputStream.writeObject(returnData);
            objectOutputStream.flush();
        } catch (IOException e) {
            ReturnData returnData = new ReturnData(false, "error : IO exception occurs");
            try {
                objectOutputStream.writeObject(returnData);
                objectOutputStream.flush();
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        } catch (Exception e) {
            ReturnData returnData = new ReturnData(false, "error: " + e.getMessage());
            try {
                objectOutputStream.writeObject(returnData);
                objectOutputStream.flush();
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }
    }
}
